package com.ww.java8.fp.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class FlatMapDemo {
	
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		String words = "public static void main";
		Stream<String> stream = Pattern.compile("\\PL+").splitAsStream(words);
		// 得到的流为[...["y","o","u"],["b","o","a","t"],...]
		Stream<Stream<String>> result = stream.map(w -> letters(w));
		// 得到的流为[..."y","o","u","b","o","a","t",...]
		// 产生一个流，它是通过将mapper应用于当前流中所有元素所产生的结果连接到一起（注意，这里的每个结果都是一个流）
		Stream<String> flatResult = stream.flatMap(w -> letters(w));
	}

	public static Stream<String> letters(String s) {
		List<String> result = new ArrayList<>();
		for (int i = 0; i < s.length(); i++) {
			result.add(s.substring(i, i+ 1));
		}
		return result.stream();
	}
}
